<?php

namespace App\Trip\Orders\BookOrders\Plane;

use App\Trip\Enums\Plane\PlaneProviders;
use App\Trip\Orders\TransferFlag;
use App\Trip\Orders\TripType;
use App\Trip\Plane\Interfaces\Result\ChangeFlightInfoInterface;
use App\Trip\Plane\PlaneInfo\FlightInfo;
use Exception;

class BuildChangeOrderData
{
    private ChangeFlightInfoInterface $seatItem;
    private string                    $change_key;

    /**
     * @throws Exception
     */
    public function __construct(PlaneProviders $provider, string $change_key)
    {
        $this->change_key = $change_key;
        $service          = $provider->getPlaneQueryStrategy();
        $this->seatItem   = $service->getChangeSeatItem($this->change_key);
    }

    public function getFlightInfo(): FlightInfo
    {
        $flight_info = $this->seatItem->getFlightInfo();
        $flight_info->setCabinName($this->seatItem->getCabinName());
        $flight_info->setCabin($this->seatItem->getCabin());
        $flight_info->setCabins($this->seatItem->getCabins());
        return $flight_info;
    }

    public function getOrderData(): array
    {
        $seatItem = $this->seatItem;
        return [
            'change_key'         => $this->change_key,
            'flight_no'          => $seatItem->getFlightNo(),
            'from_code'          => $seatItem->getDepartCode(),
            'to_code'            => $seatItem->getArriveCode(),
            'from_name'          => $seatItem->getDepartName(),
            'to_name'            => $seatItem->getArriveName(),
            'short_from_name'    => $seatItem->getDepartName(true),
            'short_to_name'      => $seatItem->getArriveName(true),
            'from_tower'         => $seatItem->getFromTower(),
            'to_tower'           => $seatItem->getToTower(),
            'cabin'              => $seatItem->getCabin(),
            'from_name_uy'       => $seatItem->getDepartNameUy(),
            'to_name_uy'         => $seatItem->getArriveNameUy(),
            'short_from_name_uy' => $seatItem->getDepartNameUy(true),
            'short_to_name_uy'   => $seatItem->getArriveNameUy(true),
            'start_at'           => $seatItem->getDepartAt(),
            'arrive_at'          => $seatItem->getArriveAt(),
            'trip_no'            => 1,
            'trip_type'          => TripType::TRIP_TYPE_PLANE,
            'transfer_flag'      => TransferFlag::ORDER_TRANSFER_FLAG_NONE,
            'extension'          => $seatItem->getExtension(),
        ];
    }
}